package com.jiaai.shop.store.api;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jiaai.shop.store.entity.*;
import com.jiaai.shop.store.mapper.SExtractMoneyMapper;
import com.jiaai.shop.store.mapper.SUserMapper;
import com.jiaai.shop.store.service.*;
import com.jiaai.shop.util.EmailTest;
import com.jiaai.shop.util.MD5;
import com.jiaai.wgj.common.json.RestResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/admin/store")
public class AdminController {
    @Autowired
    private ISGoodsService isGoodsService;

    @Autowired
    private ISMemberService isMemberService;

    @Autowired
    private ISUserService isUserService;

    @Autowired
    private ISExtractMoneyService isExtractMoneyService;

    @Autowired
    private ISStepsService isStepsService;

    @Autowired
    private SExtractMoneyMapper sExtractMoneyMapper;

    @Autowired
    private SUserMapper suserMapper;

    @Autowired
    private ISSystemconfigService isSystemconfigService;

    @Autowired
    private ISOrderService isOrderService;

    @Autowired
    private ITBankService bankService;
    /**
     * 商品添加和修改
     * 传入对象
     * */
    @PostMapping("/goodsInsertOrUpdate")
    Object goodsInsertOrUpdate(@RequestBody SGoods sGoods)throws Exception{
        try {
            sGoods.setTime(new Date());
            return RestResultGenerator.genSuccessResult(isGoodsService.insertOrUpdate(sGoods));
        }catch (Exception e){
            return RestResultGenerator.genErrorResult("");
        }
    }

    /**
     * 商品删除
     * 传入商品id
     * */
    @GetMapping("/deleteGoods")
    Object deleteGoods(@RequestParam int id)throws Exception{
        try {
            return RestResultGenerator.genSuccessResult(isGoodsService.deleteById(id));
        }catch (Exception e){
            return RestResultGenerator.genErrorResult("");
        }
    }

    /**
     * 查找商品
     * */
    @GetMapping("/seachGoods")
    Object seachGoods(@RequestParam Integer curr,SGoods sGoods)throws Exception{
        try {
            EntityWrapper wrapper =new EntityWrapper();
            wrapper.like("goodsName",sGoods.getGoodsName());
            if(sGoods.getState()!=null) {
                wrapper.eq("state", sGoods.getState());
            }
            Page page=new Page(curr,8);
          return RestResultGenerator.genSuccessResult(isGoodsService.findGoodsAll(page,wrapper));
        }catch (Exception e){
            return RestResultGenerator.genErrorResult("");
        }
    }

    /**
     * 搜索单个商品
     * 传入商品id
     * */
    @GetMapping("/seachOneGoods")
    Object seachIdGoods(@RequestParam int id)throws Exception{
        try {
            return RestResultGenerator.genSuccessResult(isGoodsService.selectById(id));
        }catch (Exception e){
            return RestResultGenerator.genErrorResult("");
        }
    }


    /**
     * 后台登录
     * */
    @RequestMapping("/login")
    public Object login(@RequestParam String mId,@RequestParam String password)throws Exception{
        try {
            return RestResultGenerator.genSuccessResult(isUserService.login(mId,password));
        }catch (Exception e){
            return RestResultGenerator.genErrorResult("");
        }
    }

    /**
     * 后台注册
     * 不对页面开放
     * */
    @RequestMapping("/reg")
    public Object reg(@RequestBody SUser sUser)throws Exception{
        try {
            return RestResultGenerator.genSuccessResult(isUserService.rege(sUser));
        }catch (Exception e){
            return RestResultGenerator.genErrorResult("");
        }
    }
    @RequestMapping("/bankinfo/{bankId}")
    public  Object inof(@PathVariable("bankId") Integer bankId){
        return  RestResultGenerator.genSuccessResult(bankService.selectById(bankId));
    }

    /**
     * 提现处理，更改提现状态
     *
     * */
    @PostMapping("/updateExtract")
    public Object updateExtract(String id,Integer status)throws Exception{
        try {
            SExtractMoney sExtractMoney=isExtractMoneyService.selectById(id);
            sExtractMoney.setState(status);
            isExtractMoneyService.updateById(sExtractMoney);
            return RestResultGenerator.genSuccessResult("ok");
        }catch (Exception e){
            return RestResultGenerator.genErrorResult("error");
        }
    }
    @PostMapping("/updatabankstatus")
    public Object upDataBankStatus( String id,Integer status)throws Exception{
        try {
            SExtractMoney extractMoney=isExtractMoneyService.selectById(id);
            extractMoney.setResults(status);
            extractMoney.setResultsTime(new Date());
            return RestResultGenerator.genSuccessResult(sExtractMoneyMapper.updateById(extractMoney));
        }catch (Exception e){
            return RestResultGenerator.genErrorResult("");
        }
    }
    /**
     * 搜索提现申请信息
     * */
    @GetMapping("/seachExtract")
    public Object seachExtract( SearchExtractVo searchExtractVo){
        try {
            Page page=new Page(searchExtractVo.getCurr(),20);
            return RestResultGenerator.genSuccessResult(isExtractMoneyService.List(page,searchExtractVo));
        }catch (Exception e){
            return RestResultGenerator.genErrorResult(e.getMessage());
        }
    }

    /**
     * 插入步骤流程图
     * */
    @PostMapping("/insertOrUpdateStep")
    public Object insertOrUpdateStep(@RequestBody SSteps sSteps)throws Exception{
        try {
            return RestResultGenerator.genSuccessResult(isStepsService.insertOrUpdate(sSteps));
        }catch (Exception e){
            return RestResultGenerator.genErrorResult("");
        }
    }

    /**
     * 删除流程
     * */
    @GetMapping("/deleteStep")
    public Object deleteStep(@RequestParam int id)throws Exception{
        try {
            return RestResultGenerator.genSuccessResult(isStepsService.deleteById(id));
        }catch (Exception e){
            return RestResultGenerator.genErrorResult("");
        }
    }

    /**
     * 查询流程步骤
     * */
    @GetMapping("/seachStep")
    public Object seachStep()throws Exception{
        try {
            return RestResultGenerator.genSuccessResult(isStepsService.seachStep());
        }catch (Exception e){
            return RestResultGenerator.genErrorResult("");
        }
    }

    /**
     * 查询单个流程
     * */
    @GetMapping("/seachOneStep")
    public Object seachOneStep(@RequestParam int id)throws Exception{
        try {
            return RestResultGenerator.genSuccessResult(isStepsService.selectById(id));
        }catch (Exception e){
            return RestResultGenerator.genErrorResult("");
        }
    }





    /**
     * 查找会员
     * */
    @GetMapping("/seachMember")
    public Object seachMember(@RequestParam int curr,SMember sMember)throws Exception{
        try {
            EntityWrapper wrapper =new EntityWrapper();
            wrapper.like("memberName",sMember.getMemberName());
            if(sMember.getType()!=null) {
                wrapper.eq("type", sMember.getType());
            }
            Page page=new Page(curr,8);
            return RestResultGenerator.genSuccessResult(isMemberService.seachMember(page,wrapper));
        }catch (Exception e){
            return RestResultGenerator.genErrorResult("");
        }
    }

    /**
     * 修改系统设置
     * 前端的id限定死为1
     * */
    @PostMapping("/updateSystem")
    public Object updateSystem(@RequestBody SSystemconfig sSystemconfig){
        try {
            return RestResultGenerator.genSuccessResult(isSystemconfigService.insertOrUpdate(sSystemconfig));
        }catch (Exception e){
            return RestResultGenerator.genErrorResult("");
        }
    }

    /**
     * 修改用户表
     * */
    @PostMapping("/updateuser")
    public Object updateuser(@RequestBody SUser sUser){
        try {
            sUser.setPassword(MD5.MD5Encode(sUser.getPassword()));
            return RestResultGenerator.genSuccessResult(isUserService.insertOrUpdate(sUser));
        }catch (Exception e){
            return RestResultGenerator.genErrorResult("");
        }
    }



    /**
     * 根据会员账号查找信息
     *
     * */
    @PostMapping("/getUserAll")
    public Object getUserAll(@RequestParam String user)throws Exception{
        try {
            return RestResultGenerator.genSuccessResult(suserMapper.userAll(user));
        }catch (Exception e){
            return RestResultGenerator.genErrorResult("");
        }
    }
    /**
     * 订单查询
     *
     * */
   @PostMapping("/seachOrder")
    public Object seachOrder(@RequestParam int curr)throws Exception{
        try {
           Page page=new Page(curr,8);
            return RestResultGenerator.genSuccessResult(isOrderService.seachOrder(page));
        }catch (Exception e){
            return RestResultGenerator.genErrorResult("");
        }
    }

}
